package com.demo.utils;

import org.junit.Test;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @author gy
 * @date 2024/4/24
 */
public class SimpleDateFormatUtils {

    /**
     * 获取当日开始时间
     */
    @Test
    public void m5(){
        long startTime = System.currentTimeMillis();

        String time = new SimpleDateFormat("yyyy-MM-dd 00:00:00").format(new Date());
        System.out.println(time);

        long endTime = System.currentTimeMillis();
        System.out.println("执行耗时["+(endTime - startTime)+"]毫秒");
    }

    /**
     * 获取整点开始时间
     */
    @Test
    public void m2(){
        long startTime = System.currentTimeMillis();

        String time = new SimpleDateFormat("yyyy-MM-dd HH:00:00").format(new Date());
        System.out.println(time);

        long endTime = System.currentTimeMillis();
        System.out.println("执行耗时["+(endTime - startTime)+"]毫秒");
    }

    /**
     * 获取当日结束时间
     */
    @Test
    public void m3(){
        long startTime = System.currentTimeMillis();

        String time = new SimpleDateFormat("yyyy-MM-dd 23:59:59").format(new Date());
        System.out.println(time);

        long endTime = System.currentTimeMillis();
        System.out.println("执行耗时["+(endTime - startTime)+"]毫秒");
    }

    /**
     * 获取 一月期限的开始时间
     *
     * @return date
     */
    public static String getOneMonthStartTime(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd 00:00:00");
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.add(Calendar.DAY_OF_WEEK,-1);
        c.add(Calendar.MONTH,-1);
        return sdf.format(c.getTime());
    }

    /**
     * 获取 一月期限的结束时间
     *
     * @return date
     */
    public static String getOneMonthEndTime(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd 23:59:59");
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.add(Calendar.DAY_OF_WEEK,-1);
        return sdf.format(c.getTime());
    }

}
